﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using ZLCMS.Data.Model;
using NHibernate;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Data.Common;

namespace ZLCMS.Web.SystemManage.Basics.MessageBoard
{
    public partial class MessageDelete : System.Web.UI.Page
    {
        private FrameHelper _framePage;

        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new FrameHelper(this);

            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                if (Request["ajax"] == "dels")
                {
                    String ids = Request["ids"];
                    if (!String.IsNullOrEmpty(ids))
                    {
                        String[] idArray = ids.Split(',');
                        foreach (String id in idArray)
                        {
                            DeleteMsg(Int32.Parse(id), session);
                        }
                    }
                }
                else if (!String.IsNullOrEmpty(Request["uid"]))
                {
                    DeleteMsg(Int32.Parse(Request["uid"]), session);
                }
                else
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作失败-参数错误!" });
                }
            }

            _framePage.Alert(new ReturnCode() { statusCode = 200, message = "操作成功!", navTabId = _framePage.NavTabId });
        }

        private void DeleteMsg(int msgId, ISession session)
        {
            ITransaction tran = session.BeginTransaction();
            try
            {
                session.Delete(new MessageInfo() { Id = msgId });
                session.Delete("from MessageReply where Messageid = ?", msgId, NHibernateUtil.Int32);

                tran.Commit();
            }
            catch (System.Exception ex)
            {
                tran.Rollback();
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作失败-" + ex.Message });
                return;
            }
        }
    }
}
